package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * @Classname RentStartTimePlaceHandler
 * @Description 五大双线落地方产品，订购时，起租时间从订单带出，不可修改
 * @Author zhang.song
 * @Date 2021-07-14 18:18
 */
@Service
public class RentStartTimePlaceHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttrVO) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        String serviceOfferId = Optional.ofNullable(querySceneInstService.getSceneInst(sceneInstId)).map(KiteSceneInst::getServiceOfferId).orElse("");
        if(KiteStringUtils.equals(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER)) {
            commonAttrVO.setIsEdit(KeyConsts.IFTRUE_F);
        } else {
            commonAttrVO.setIsEdit(KeyConsts.IFTRUE_T);
        }

        KiteAttr startRentDateAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.START_RENT_DATE);
        if (Objects.nonNull(startRentDateAttr)) {
            startRentDateAttr.setIsEdit(commonAttrVO.getIsEdit());
            operateSceneInstService.setCommonAttr(sceneInstId, startRentDateAttr);
        }
    }
}
